package com.shizhanzhe.zxbapp.home;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FriendDetailsActivity extends AppCompatActivity {
    @BindView(R.id.top_back)
    ImageButton topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.ll_no)
    LinearLayout llNo;
    private InvitationAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ti_record);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("我的好友");
        initData();
        setListener();
    }

    private void initData() {
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new InvitationAdapter(FriendDetailsActivity.this);
        recycler.setAdapter(mAdapter);
        inviteFriends(UserBean.uid);
    }

    private void setListener() {
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void inviteFriends(int uid) {
        OkHttpDownloadJsonUtil.downloadJson(FriendDetailsActivity.this, Path.inviteFriends + "?uid=" + uid, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    InviteFriend tiBean = gson.fromJson(json, InviteFriend.class);
                    if (tiBean.getData().size() > 0) {
                        mAdapter.updateData(tiBean.getData());
                    } else {
                        llNo.setVisibility(View.VISIBLE);
                        recycler.setVisibility(View.GONE);
                    }
                } else {
                    Toast.makeText(FriendDetailsActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
